let vue = new Vue({
    el: '#fixedData',
    data: {
        webname: webname,
        menu: [],
        status: 0,
        active: 'active',
        address: [],
    },
    mounted() {
        //加载左侧菜单
        let data = sessionStorage.menu;

        if (!data) {
            $.ajax({
                url: menuUrl,
                dataType: 'text',
                success: (menu) => {
                    menu = eval('(' + menu + ')');
                    sessionStorage.menu = JSON.stringify(menu);
                    this.menu = menu;
                    this.thisActive();
                    this.thisAttr();
                }
            })
        } else {
            this.menu = JSON.parse(data);
            this.thisActive();
            this.thisAttr();
            console.log(JSON.parse(data),'data')
        }


    },
    methods: {
        //记住收展
        onActive: function (pid, id = false) {
            let data;
            if (id === false) {
                data = this.menu[pid];

                if (data.url.length > 0) {
                    this.menu.forEach((v, k) => {
                        v.active = false;
                        v.list.forEach((v2, k2) => {
                            v2.active = false;
                        })
                    })

                    data.active = true;

                }

                data.hidden = !data.hidden;

            } else {

                this.menu.forEach((v, k) => {
                    v.active = false;
                    v.list.forEach((v2, k2) => {
                        v2.active = false;
                    })
                })

                data = this.menu[pid].list[id];
            }
            this.updateStorage();
            if (data.url.length > 0) {
                window.location.href = data.url;
            }
        },

        //更新菜单缓存
        updateStorage() {
            sessionStorage.menu = JSON.stringify(this.menu);
        },
        //菜单高亮
        thisActive: function () {
            let pathname = window.location.pathname;
            let host = window.location.host;
            let pid = false;
            let id = false;
            console.log(pathname,'pathname')
            console.log(host,'host')
            this.menu.forEach((v, k) => {
                let url = v.url;
                if (url.length > 0) {
                    if (url[0] != '/' || url.substr(0, 4) != 'http') {
                        url = '/' + url;
                    }
                }
                if (pathname === url) {
                    pid = k;
                }
                v.list.forEach((v2, k2) => {
                    let url = v2.url;
                    // console.log(url,'v2.url')
                    // if (url.length > 0) {
                    //     if (url[0] !== '/' || url.substr(0, 4) !== 'http') {
                    //         url = '/' + url;
                    //     }
                    // }
                    // console.log(pathname,'pathname555')
                    //当路由等于点击的按钮时
                    if (pathname === url) {
                        // debugger;
                        pid = k;
                        id = k2;
                        // console.log(JSON.parse(data))
                    }
                })
            })


            if (id !== false) {
                this.menu[pid].list[id].active = true;
            } else {
                if (pid !== false) {
                    this.menu[pid].active = true;
                }
            }

            this.updateStorage();

        },
        //当前位置
        thisAttr: function () {
            // 当前位置
            let address = [{
                name: '主菜',
                url: '/toCommodityList',
            }];
            this.menu.forEach((v, k) => {
                v.list.forEach((v2, k2) => {
                    if (v2.active) {
                        address.push({
                            name: v.name,
                            url: 'javascript:;'
                        })
                        address.push({
                            name: v2.name,
                            url: v2.url,
                        })
                        this.address = address;
                    }
                })
            })
        },
    },
})